﻿#include <windows.h>
#include <d2d1_1.h>
#include <dwrite.h>

#pragma comment(lib, "d2d1.lib")
#pragma comment(lib, "dwrite.lib")
#pragma comment(lib, "dxguid.lib")
namespace DarkMoonUI {

    
    class zyD2DRenderer {
    private:
        HWND m_hWnd;                        // 关联窗口句柄
        D2D1_SIZE_U m_size;                  // 渲染区域尺寸
        ID2D1Factory1* m_pD2DFactory = nullptr; // D2D工厂
        IDWriteFactory* m_pDWriteFactory = nullptr; // DirectWrite工厂
        ID2D1HwndRenderTarget* m_pRenderTarget = nullptr; // 主渲染目标
        ID2D1SolidColorBrush* m_pDefaultBrush = nullptr;  // 默认画刷

        void CreateDeviceResources();

    public:
        zyD2DRenderer(HWND hWnd, UINT width, UINT height);

        ~zyD2DRenderer();
        bool SetD2DFactory1(ID2D1Factory1* pD2DFactory);
        ID2D1Factory1* GetD2DFactory1() const;

        ID2D1SolidColorBrush* GetDefaultBrush() const;
        void BeginDraw();

        void EndDraw();
        void Clear(D2D1_COLOR_F color = D2D1::ColorF(D2D1::ColorF::White));

        void DrawRectangle(const D2D1_RECT_F& rect, D2D1_COLOR_F color, float strokeWidth = 1.0f);

        void FillRectangle(const D2D1_RECT_F& rect, D2D1_COLOR_F color);

        void DrawCircle(D2D1_POINT_2F center, float radius,D2D1_COLOR_F color, float strokeWidth = 1.0f);

        void FillCircle(D2D1_POINT_2F center, float radius, D2D1_COLOR_F color);

        void DrawText(const WCHAR* text,const D2D1_RECT_F& layoutRect,
            IDWriteTextFormat* pFormat, D2D1_COLOR_F color);

        IDWriteTextFormat* CreateTextFormat(const WCHAR* fontName,float fontSize,
            DWRITE_FONT_WEIGHT fontWeight = DWRITE_FONT_WEIGHT_NORMAL,
            DWRITE_FONT_STYLE fontStyle = DWRITE_FONT_STYLE_NORMAL,
            DWRITE_FONT_STRETCH fontStretch = DWRITE_FONT_STRETCH_NORMAL);
        void FillRoundedRectangle(const D2D1_RECT_F& rect, float radiusX, float radiusY, D2D1_COLOR_F color);
        void Resize(UINT width, UINT height);
            
        void DrawShadow(const D2D1_RECT_F& rect, const float 圆角度 , float 阴影模糊度, D2D1_COLOR_F 阴影颜色, D2D1_POINT_2F 阴影偏移度, float 阴影透明度);
    };
    template<class T>
    inline void SafeRelease(T*& pInterfaceToRelease) {
        if (pInterfaceToRelease != nullptr) {
            pInterfaceToRelease->Release();
            pInterfaceToRelease = nullptr;
        }
    }
}